package com.boss.thirteen.service.impl;

import com.boss.common.entity.PageDTO;
import com.boss.common.util.DateUtils;
import com.boss.framework.dto.PaginationDTO;
import com.boss.salary.constant.SystemDictConstant;
import com.boss.salary.dao.IAgencyInfoDao;
import com.boss.salary.entity.AgencyInfo;
import com.boss.sys.ca.user.dto.UserDTO;
import com.boss.sys.utils.SysUtils;
import com.boss.thirteen.dao.ISalaryThirteenMouldDao;
import com.boss.thirteen.dao.IThirteenAgencyVerifyDao;
import com.boss.thirteen.entity.ThirteenAgencyVerify;
import com.boss.thirteen.service.IThirteenMouldService;
import com.boss.thirteen.vo.ThirteenMouldVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class ThirteenMouldServiceImpl implements IThirteenMouldService {
    @Autowired
    private ISalaryThirteenMouldDao thirteenMouldDao;
    @Autowired
    private IThirteenAgencyVerifyDao thirteenAgencyVerifyDao;
    @Autowired
    private IAgencyInfoDao agencyInfoDao;
    @Override
    public PageDTO<Map<String, Object>> querySalaryThirteenMouldPage(PaginationDTO paginationDTO, String year, String province) {
        List<Map<String, Object>> moulds=thirteenMouldDao.querySalaryThirteenMouldPage(paginationDTO,year,province);
        int total=thirteenMouldDao.querySalaryThirteenMouldCount(year,province);
        PageDTO<Map<String, Object>> PageDTO = new PageDTO<>();
        PageDTO.setRows(moulds);
        PageDTO.setTotal(total);
        return PageDTO;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insertMould(List<ThirteenAgencyVerify> tavs, List<Map<String, Object>> mouldMap, UserDTO userDTO) {
        List<AgencyInfo> agencyInfos= agencyInfoDao.getAgencyTreeData(null,userDTO.getProvince());
        Map<String,String> agencyIdNameMap=agencyInfos.stream().collect(Collectors.toMap(AgencyInfo::getId, AgencyInfo::getAgencyName));
        Map<String,String> agencyIdCodeMap=agencyInfos.stream().collect(Collectors.toMap(AgencyInfo::getId, AgencyInfo::getAgencyCode));
        thirteenMouldDao.batchInsertSalaryThirteenMould(mouldMap);
        for(ThirteenAgencyVerify tv:tavs){
            tv.setAgencyCode(agencyIdCodeMap.get(tv.getAgencyId()));
            tv.setAgencyName(agencyIdNameMap.get(tv.getAgencyId()));
        }
        thirteenAgencyVerifyDao.batchInsertThirteenAgencyVerify(tavs);
    }

    @Override
    public void updateMould(ThirteenMouldVO tv) {
        String mouldKey=tv.getMoulds().get(0).get("mouldKey").toString();
        String mofDivCode=tv.getMoulds().get(0).get("mof_div_code").toString();
        String year= String.valueOf(Calendar.getInstance().get(Calendar.YEAR));
        thirteenMouldDao.batchUpdateSalaryThirteenMould(tv.getMoulds());
        List<String> hadAgencyIds=new ArrayList<>();
        List<ThirteenAgencyVerify> hadAgencys=new ArrayList<>();
        for(String agencgId:tv.getAgencyIds()){
            for(ThirteenAgencyVerify tav:tv.getThirteenAgencyVerify()){
                if(agencgId.equals(tav.getAgencyId())){
                    hadAgencyIds.add(agencgId);
                    hadAgencys.add(tav);
                }
            }
        }
        tv.getThirteenAgencyVerify().removeAll(hadAgencys);
        tv.getAgencyIds().removeAll(hadAgencyIds);
        for(ThirteenAgencyVerify tav:tv.getThirteenAgencyVerify()){
            tav.setIsDeleted(SystemDictConstant.IS_DELETED);
        }
        List<AgencyInfo> agencyInfos= agencyInfoDao.getAgencyTreeData(null,mofDivCode);
        Map<String,String> agencyIdNameMap=agencyInfos.stream().collect(Collectors.toMap(AgencyInfo::getId, AgencyInfo::getAgencyName));
        Map<String,String> agencyIdCodeMap=agencyInfos.stream().collect(Collectors.toMap(AgencyInfo::getId, AgencyInfo::getAgencyCode));
        for(String agencgId:tv.getAgencyIds()){
            String uuid= SysUtils.generateGuid();
            ThirteenAgencyVerify tav=new ThirteenAgencyVerify();
            tav.setId(uuid);
            tav.setBizKey(uuid);
            tav.setMouldKey(mouldKey);
            tav.setCreateTime(String.valueOf(DateUtils.getCurrentTimeStamp()));
            tav.setAgencyId(agencgId);
            tav.setIsWorkflowInit(SystemDictConstant.IS_FALSE);
            tav.setMofDivCode(mofDivCode);
            tav.setYear(year);
            tav.setIsDeleted(SystemDictConstant.NOT_DELETED);
            tav.setAgencyCode(agencyIdCodeMap.get(agencgId));
            tav.setAgencyName(agencyIdNameMap.get(agencgId));
            tv.getThirteenAgencyVerify().add(tav);

        }
        thirteenAgencyVerifyDao.batchUpdateThirteenAgencyVerify(tv.getThirteenAgencyVerify());

    }

    @Override
    public void deleteMould(String mouldKey) {

    }

    @Override
    public List<Map<String, Object>> queryMould(Map<String, Object> queryParam) {

        return null;
    }

    @Override
    public List<ThirteenAgencyVerify> queryThirteenAgency(Map<String, Object> queryParam) {
        return  thirteenAgencyVerifyDao.selectThirteenAgencyVerify(queryParam);
    }

}
